package com.jy.appmarket.adapter;

import com.jy.appmarket.R;

import android.content.Context;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.TextView;
import it.gmariotti.cardslib.library.internal.CardArrayAdapter;
import it.gmariotti.cardslib.library.prototypes.CardSection;
import it.gmariotti.cardslib.library.prototypes.SectionedCardAdapter;

public class ApkFileManagerSectiondAdapter extends SectionedCardAdapter {

	private ApkFileManagerSectionClickCallBack mListener;
	
	public interface ApkFileManagerSectionClickCallBack {
		
		void onApkFileSectionCheckBoxClick(Boolean select, int type);
	}
	
	public ApkFileManagerSectiondAdapter(Context context, CardArrayAdapter cardArrayAdapter, ApkFileManagerSectionClickCallBack callback) {
		super(context, R.layout.apk_file_ui_card_section_layout, cardArrayAdapter);
		mListener = callback;
	}

	@Override
	protected View getSectionView(int position, View view, ViewGroup parent) {
		final ApkFileUiSection section = (ApkFileUiSection) getCardSections().get(position);
		if (section != null) {
			TextView title = view.findViewById(R.id.apk_file_ui_section_title);
			if (title != null) {
				title.setText(section.getTitle());
			}
			final CheckBox select = view.findViewById(R.id.apk_file_ui_section_check_box);
			select.setChecked(section.getmSelect());
			select.setOnClickListener(new OnClickListener() {
				
				@Override
				public void onClick(View v) {
					section.setmSelect(select.isChecked());
					if(mListener != null){
						mListener.onApkFileSectionCheckBoxClick(select.isChecked(), section.getmType());
					}
					
				}
			});
//			select.setOnCheckedChangeListener(new OnCheckedChangeListener() {
//
//				@Override
//				public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
//					mListener.onApkFileSectionCheckBoxClick(isChecked, section.getmType());
//				}
//			});
		}
		return view;
	}
	
	public static class ApkFileUiSection extends CardSection {
		private boolean mSelect;
		private int mType; // 0：建议清理，1：建议保留

		public ApkFileUiSection(int firstPosition, CharSequence title, Boolean selectAll, int type) {
			super(firstPosition, title);
			setmSelect(selectAll);
			setmType(type);
		}

		public Boolean getmSelect() {
			return mSelect;
		}

		public void setmSelect(Boolean mSelect) {
			this.mSelect = mSelect;
		}

		public int getmType() {
			return mType;
		}

		public void setmType(int mType) {
			this.mType = mType;
		}
	}
}
